<!--
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-04-07 20:18:30
 * @LastEditors: Sam
 * @LastEditTime: 2022-05-05 22:11:48
-->
<template>
    <div class="app-container">
        <el-card>
            <div slot="header" class="clearfix">
                <span>秒杀列表</span>
            </div>
             <div class="filter-container">
            <el-input v-model="listQuery.title" placeholder="请输入标题" clearable style="width: 200px;margin-right: 10px;margin-bottom:10px" class="filter-item" @keyup.enter.native="handleFilter" />

                <el-button v-waves  class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
                {{ '搜索' }}
                </el-button>
                 <el-button v-waves :disabled="!checkPermission(['bargain.create'])"  class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
                     {{ "添加" }}
                </el-button>     
            </div>
             <el-table
                v-loading="listLoading"
                :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                :data="list"
                border
                fit
                @select="handleSelect"
                @select-all ="selectAll"
                highlight-current-row
                style="width: 100%"
                size="mini"
                >  
                 <!-- <el-table-column align="center" type="selection" width="55"> 
                </el-table-column> --> 
            <el-table-column  align="center" label="标题">
                <template slot-scope="scope">
                    <span>{{ scope.row.title }}</span>
                </template>
            </el-table-column>
            <el-table-column  align="center" label="开始时间">
                <template slot-scope="scope">
                    <span>{{ scope.row.start_time | parseTime }}</span>
                </template>
            </el-table-column>
            <el-table-column  align="center" label="结束时间">
                <template slot-scope="scope">
                    <span>{{ scope.row.end_time | parseTime}}</span>
                </template>
            </el-table-column>
            <el-table-column  align="center" label="商品图片">
                <template slot-scope="scope">
                    <el-image   :preview-src-list="scope.row.goods_thumb ? scope.row.goods_thumb.split(',') : []" style="width: 50px; height: 50px" :src="scope.row.goods_thumb" fit="cover"></el-image>
                </template>
            </el-table-column>
            <el-table-column  align="center" label="商品原价">
                <template slot-scope="scope">
                    <span>{{ scope.row.goods_price }}</span>
                </template>
            </el-table-column>
            <el-table-column  align="center" label="库存">
                <template slot-scope="scope">
                    <span>{{ scope.row.stock }}</span>
                </template>
            </el-table-column>
            <el-table-column  align="center" label="秒杀价格">
                <template slot-scope="scope">
                    <span>{{ scope.row.price }}</span>
                </template>
            </el-table-column>
           
            
            <el-table-column  align="center" label="上架">
                <template slot-scope="scope">
                      <el-tag   v-if="scope.row.is_show == '1'" type="success" size="mini">
                    开 
                    </el-tag>
                    <el-tag   v-else type="error" size="mini">
                     开 
                    </el-tag>
                </template>
            </el-table-column>


                <el-table-column align="center" :label="'操作'" width="250">
                <template slot-scope="scope">
                    
                    <el-button v-waves v-permission="['bargain.edit']"  type="primary"  size='mini' icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">
                        {{ "编辑" }}
                    </el-button>
                    <el-button v-waves v-permission="['bargain.delete']"  type="danger" :destroy-on-close="true" size='mini' icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">
                            {{ "删除" }}
                    </el-button>
                </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
        </el-card>
          <el-dialog :title="'详情'" :visible.sync="detail.dialogVisible" v-if="detail.dialogVisible">
            <detail :data="detail.data" />
         </el-dialog>
           <el-dialog :title="'编辑'" :visible.sync="edit.dialogVisible" @close="getList" :close-on-click-modal=false>
            <edit :item="edit" />
         </el-dialog>
        <el-dialog :title="'添加'" :visible.sync="create.dialogVisible" @close="getList"  :close-on-click-modal=false>
            <create :item="create" />
        </el-dialog>
    </div>
</template>
<style scoped>
    .import {
        display: inline-block;
        margin-left: 5px;
    }
</style>
<script>
    import {export_excell} from '@/api/export_excell' // 导出
    import waves from '@/directive/waves' // waves directive
    import permission from '@/directive/permission/index.js' // 权限判断指令
    import checkPermission from '@/utils/permission' // 权限判断函数
    import { list, detail, del } from '@/api/seckill' // api接口
    import Detail from '@/components/Detail/detail' // 详情组件
    import Create from './components/create' // 添加组件
    import Edit from './components/edit' // 编辑组件
    import { parseTime } from '@/utils' // 时间格式化函数
    import { getToken } from '@/utils/auth' // 获取token
    import Pagination from '@/components/Pagination' // 分页组件
    export default{
        name: 'bargainIndex',
        components: { Pagination,Detail,Create,Edit }, // 引入组件
        filters: {
            parseTime // 时间过滤器
        },
        directives: { waves, permission }, // 引入指令
          data() {
            return {
             headerObj: {
                'Authorization': 'Bearer ' + getToken()
            },
            list: null, // 列表数据
            total: 0, // 总条数
            select_ids : [], // 选中的id集合
            listLoading: true, //加载loadig
            listQuery: { //列表查询参数
                page: 1,
                limit: 10
            },
            create: { // 新增
                dialogVisible: false
            },
            detail: { // 详情
                dialogVisible: false,
                data: []
            },
            edit: { // 编辑
                dialogVisible: false,
                id: ''
            }
            }
        },
          created() {
            this.getList()
        },
        methods: {
            beforeImport(){ //导入前回调 
                this.listLoading = true;
            },
            
            handleImport(res) { // 导入
                if(!res.code) {
                    this.$message.error(res.msg)
                    this.listLoading = false;
                } else {
                    this.listLoading = false;
                    this.$message.success(res.msg);
                    this.getList()
                }
            },
            handleSelect(selection, row) { // 选中
                 this.select_ids = [];
                selection.map(item => {
                     this.select_ids.push(item.id);
                })
            },
             selectAll(selection) { // 全选
                 this.select_ids = [];
                selection.map(item => {
                     this.select_ids.push(item.id);
                })
            },

            handleExport(){
                export_excell(process.env.VUE_APP_BASE_API + 'admin/bargain_export',this.listQuery);
            },
            checkPermission, // 权限判断函数
            handleFilter() { // 查询
                this.listQuery.page = 1
                this.getList()
            },
            handleCreate() { // 新增对话框显示
              this.create.dialogVisible = true
            },
            handleEdit(index, row) { // 编辑对话框显示
              this.edit.dialogVisible = true
              this.edit.id = row.id
            },
            handleBatch() {  // 批量删除
                var that = this;
                if(this.select_ids.length == 0){
                    this.$message({
                        message: '请选择要删除的数据',
                        type: 'warning'
                    });
                    return false;
                }
                this.$confirm('确认删除选中的数据?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    del(this.select_ids.join(',')).then(res => {
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getList();
                    }).catch(err => {
                        this.$message({
                            message: err.msg,
                            type: 'error'
                        });
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleDelete(index, row) { // 删除
                    const thiz = this
                    this.$confirm('确认要删除吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        del(row.id).then(() => {
                        this.$message({
                            message: '删除成功',
                            type: 'success',
                            duration: 1500,
                            onClose() {
                                thiz.list.splice(index, 1)
                            }
                        })
                        })
                    }).catch(() => {

                    })
                },
            handleDetail(index, row) { // 详情
                detail(row.id).then((res) => {
                        this.detail.dialogVisible = true
                        const data = res.data
                        this.detail.data = [
                                          {
                    name:"标题",
                    content:data.title,
                    type: "text"
                },
                {
                    name:"简介",
                    content:data.sub_title,
                    type: "text"
                },
                {
                    name:"开始时间",
                    content:data.start_time,
                    type: "text"
                },
                {
                    name:"结束时间",
                    content:data.end_time,
                    type: "text"
                },
                {
                    name:"商品图片",
                    content:data.goods_thumb,
                    type: "text"
                },
                {
                    name:"商品原价",
                    content:data.goods_price,
                    type: "text"
                },
                {
                    name:"库存",
                    content:data.stock,
                    type: "text"
                },
                {
                    name:"砍价后价格",
                    content:data.price,
                    type: "text"
                },
                {
                    name:"最低砍价",
                    content:data.min_price,
                    type: "text"
                },
                {
                    name:"最高砍价",
                    content:data.max_price,
                    type: "text"
                },
                {
                    name:"是否可以自己砍 ",
                    content:data.self_bargain,
                    type: "text"
                },
                {
                    name:"详情",
                    content:data.detail,
                    type: "html"
                },
                {
                    name:"上架",
                    content:data.is_show,
                    type: "switch"
                },

                        ]
                    })
                    },
            getList() { // 获取列表
                this.listLoading = true
                list(this.listQuery).then(response => {
                        this.list = response.data.data
                        this.total = response.data.total
                        this.listLoading = false
                    })
                },
            }
    }
</script>

